libname ra "E:\data";/*cohort*/
libname pd "Z:\Medicare_20\Part_D";
libname pb "Z:\Medicare_20\Part_b";

*****Purpose:Create index dt,index medication, variable for de-escalation
     Keep IDS with at least 1 observation after index date.
     Remoove IDS with FU < is less than 6 m (part D) or 7 m (partB) 
     Remove those who switched medication in 6-7m*************
     Note : dataset ra.coh_ana1 contains Medicare beneficiaries >=66 of age with RA diagnosis identified 
          between 2009-2016 and with continuous Part a, b ,d and fee-for-service coverage  ( 1 yr before RA diagnposis
           until 2017 or death) and these beneficiaries took medication ( part B or D medication) withon 6 m of RA diagnosis
           This dataset constains multiple rows of prescription fill dates per id) ;


***************************create index date***********************************
***********first prescripion dt of primary b or d  closest to RA dt******************************;

proc sort data= ra.coh_ana1 out=coh_ana1; by bene_id rx_dt; run;

********************************************************************************************
******create index dt and keep index dt var as non-time varying var*****************
*****************************************************************************************;

data index1;
  set coh_ana1;
  if prim_b_aft6 =1 or prim_d_aft6=1;/* keep only obs with prim med after RA and within 6m ,this removes rows before ra date*/
run;

proc sort data= index1; by bene_id rx_dt;run;

data index1a; 
  set index1;
  by bene_id;
  if first.bene_id;/*first obs for prim rx*/

   index_dt=rx_dt;
   if rx_file= "d" then index_med= medication_name;
   else if rx_file="b" then index_med= med_name;

keep bene_id index_dt index_med ;
format index_dt date9. ;

run;/*n=5495, 1rps*/


***merge with cohort dataset******;
proc sort data= index1a; by bene_id;run;
proc sort data= coh_ana1; by bene_id; run;

data ra.coh_ana1;
merge  index1a coh_ana1;
by bene_id;
run;/*dataset with obs 1 yr prior to ra date*/



******************************************************************************************************
********************************DESCALATION EVENTS*****************************************************


*******************remove rows with NON BIOLOGIC RX*******************************;

data coh_ana1a;
set ra.coh_ana1;
if med= "Primary" or med= "Other" or prim_med=1 or med_group= "other_biologic";
if rx_dt ge index_dt;
run;/*remove rows with non-biologic meds and rows prior ro index date*/


******************create variables for de-escalation*************************;

proc sort data= coh_ana1a ; by bene_id;run;

***remove duplicate rows with same rx***;
data coh_ana2a;
    set coh_ana1a;

     by bene_id;

     lag_rx_dt= lag (rx_dt);

     if first.bene_id then lag_rx_dt= . ;
/*int between 2 consecutine rx*/

     rx_int = rx_dt - lag_rx_dt;

/**remove duplicate rows with same rx dates*/
     if rx_int=0 then delete;


run;


proc sort data= coh_ana2a; by bene_id;run;

/*************Setup to identify descalation: create lag vars and n_rx***********************/
data coh_ana2a;
    set coh_ana2a;

    retain bene_id n_rx;
    by bene_id;

    length lag_name_med $20 lag_dose_d $10;


/*create one var for medication name (med_name is from part B, medication_name is from part D)*/

   name_med= med_name;
   if med_name= " " then name_med= medication_name;

/*create one var for primary and other from part B and  part D*/
  if med= "Primary"  or prim_med=1 then prim_med=1;
  if med= "Other" or med_group= "other_biologic" then other_bio=1;


******************dose change*****************************************;

   dose_d1= substr (STR,1,3);
   dose_d = input (compress(dose_d1,' ','kd'),10.);

  drop dose_d1;

**lag vars**;

  lag_dose_d= lag (dose_d);

  lag_name_med = lag (name_med);
  lag_rx_file= lag (rx_file); 
  lag_prim_med= lag (prim_med);
  lag_other_bio= lag (other_bio);


if first.bene_id then do;

  lag_dose_d= " "; /*set lag variables to missing*/
  lag_name_med= " "; lag_rx_file= " ";
  lag_prim_med= " " ; lag_other_bio = " ";

  if index_dt ne rx_dt then index_rx=0;
  else if index_dt = rx_dt then index_rx=1;
 
 end;

/***time from index dt***/
  index_rx_int= rx_dt - index_dt;

/*time (days)from index dt for part b*/
  index_30 = intnx('DAY',index_dt,30);

format index_30 date9. ;

/***time(days) from index30 dt for part b***/
  index30_rx_int= rx_dt - index_30;


***switch***;
  if lag_name_med ne name_med then switch=1; 
  else if lag_name_med = name_med then switch=0;
  if lag_name_med = " " then switch= 0;

  dose_ch = dose_d - lag (dose_d);
  if first.bene_id or switch=1 then dose_ch= . ;

  format lag_rx_dt date9.;

  if index_dt = rx_dt then n_rx=1;
  else n_rx = n_rx+1;
  if rx_dt <index_dt then n_rx= .;

  if bene_death_dt ne . then dead=1;
  else if bene_death_dt = . then dead=0;


/****change etanercept QTY DISPENSED 0.004 to 4.00, 0.008 to 8.00 and 1568 to 15.68***************
***********************per discussion on meeting on 8/17/2022**************************/


   QTY_DSPNSD1 = QTY_DSPNSD_NUM;

  if name_med = "Etanercept" then do;

  if QTY_DSPNSD_NUM= 0.008 then QTY_DSPNSD1 = 8;
  if QTY_DSPNSD_NUM= 0.004 then QTY_DSPNSD1 = 4;
  if QTY_DSPNSD_NUM= 1568 then  QTY_DSPNSD1 = 15.68;

 end;


/**dose interval and dose interval difference PART D**/

  dose_int= DAYS_SUPLY_NUM/ QTY_DSPNSD1;

  dose_int1= dose_int/7;/*in weeks*/
  dose_int_w= round (dose_int1);
  lag_dose_int_w = lag (dose_int_w);

if first.bene_id then lag_dose_int_w = . ;

  diff_dose_int = dose_int_w-lag_dose_int_w;

******rx interval and diff in rx interval for int PART B**************;

***in weeks**;
  rx_int_w =  intck('week', lag_rx_dt, rx_dt, 'D');


  lag_rx_int_w = lag (rx_int_w);
  if first.bene_id then lag_rx_int_w= . ;

  diff_int_w = rx_int_w - lag_rx_int_w;


  run;

************************remove IDS WITH JUST ONE OBSERVATION AFTER RA DATE*******************;

***make n_rx (number of rows per ID)NTV*****;
proc sort data= coh_ana2a; by bene_id;run;

  data n_rx;
   set coh_ana2a (keep=bene_id n_rx);
    by bene_id;
    rename n_rx= sum_nrx;
    if last.bene_id;
    run;****173 ind had only 1 measurement ( 1 row, or no data after index date)**;
 
  
    proc sort data= coh_ana2a; by bene_id;run;
     proc sort data=n_rx; by bene_id;run;

    data coh_ana3a;

     merge coh_ana2a n_rx;

      by bene_id;

     if sum_nrx gt 1;
     run;/*for analysis use sum_nrx gt 1*/
            ****n=5322******;

proc sort data= coh_ana3a out=n nodupkey;by bene_id;run;

*************************************************************************
****************create variable to identify those who switched medication*******************
*************************************************************************;
data swit;
set coh_ana3a;
   if switch=1;
  run;

   proc sort data= swit;by bene_id;run;

   ****identify first switch***;
   data switch_ids;
   set swit;

    by bene_id;

   if first.bene_id ;/*first time switch*/

   idswitch=1;

   keep bene_id lag_rx_dt idswitch;

   rename lag_rx_dt=switch_dt;/*date of last prim med*/
   label lag_rx_dt= "date of first switch";
   run;/*n=1450 were switchers; 5332-1450 = 3882 did not switch med at any time*/

proc sort data= switch_ids;by bene_id;run;
proc sort data =coh_ana3a;by bene_id; run;

data ra.coh_ana3a;
   merge coh_ana3a switch_ids;
   by bene_id;
run;
/*n=5322*/


******************************************************************************************************
*****************Those who dont have enough FU to observe desc events**********************************
**********************atleast 1 obs after 6 months after index*******************************************
**********************(no event can happen in first 6-7m after index)*********************************;


***last fu dataset 1rps, need atleast 6-7 month of fu to be in the analytical cohort**;

proc sort data= ra.coh_ana3a out= coh_ana3a; by bene_id; run;

data last_fu;
 set coh_ana3a;
 by bene_id;
 if last.bene_id;
run;/*n=5322*/


/* time since index date at the time of last FU >6-7m*/
/*this dataset contains ids with last FU >6-7m, 1rps*/
data fu_gt67;
 set last_fu;
 keep bene_id;
 /* 6m from index dt for part d and 6 m from index30 for partb*/
 if (rx_file= "d" and index_rx_int lt 183) or (rx_file= "b" and index30_rx_int lt 183) then delete;
run;/*n=4728,(594 not enough FU, (243 from part d and 351 from part b ))*/


proc freq data= last_fu;
tables sum_nrx *rx_file;
where index_rx_int lt 183;
run;/*n=243 not enought FU to oberseve and confirm event*/

proc freq data= last_fu;
tables sum_nrx *rx_file;
where index30_rx_int lt 183;
run;/*n=351 not enough fu time to observe and confirm events*/

proc sort data= fu_gt67;by bene_id;run;
proc sort data= coh_ana3a ; by bene_id; run;

data ra.coh_ana4a;
    merge coh_ana3a (in=a) fu_gt67 (in=b);
    by bene_id;
   if b;/*include ids those with >6-7 m fu only*/
run;/*n=4728*/

***************************************************************************************************
**************THOSE WHO SWITCHED IN FIRST6-7 MONTHS, DO NOT INCLUDE IN ANALYTICAL SAMPLE*****************
************************************************************************************;


   data sw_67a;
   set ra.coh_ana4a;
/***time from index dt tp lag rx ( to capture the int until switch)***/
  
   index_lagrx_int = lag_rx_dt - index_dt;
    index30_lagrx_int = lag_rx_dt - index_30;

   if switch=1 and ((lag_rx_file="d" and index_lagrx_int lt 183) or 
      (lag_rx_file="b" and index30_lagrx_int lt 183)) ;
	  

run;
/*470 obs , 422ind switched*/
proc sort data= sw_67a out= n (keep=bene_id)nodupkey; by bene_id; run;

proc sort data= ra.coh_ana4a out= coh_ana4a; by bene_id; run;


 proc sort data= sw_67a; by bene_id;run;

 /*get list of ids of those who switched in first 6-7 months*/
data sw_67a;
   set sw_67a;
   by bene_id;
if first.bene_id;
keep bene_id ;
run;/*n=422,    b-254, d-168*/


/*remove ids of those who switched in 6-7m*/
proc sort data= sw_67a ; by bene_id;run;
proc sort data= ra.coh_ana4a out= coh_ana4a; by bene_id; run;


data coh_ana5a;
  merge coh_ana4a (in=a) sw_67a (in=b);
  by bene_id;
  if a  and not b then output;
run;
proc sort data= coh_ana5a out= n (keep=bene_id) nodupkey; by bene_id; run;/*4306*/


********************************************************************
******************create last index med date**********************;

proc sort data= coh_ana5a  ; by bene_id;run;

************switchers:date of last index prim use before switch******************;
data swit;
  set coh_ana5a;
   if idswitch=1;
   last_prim_index_dt= switch_dt;/*switch_dt is NTV and indicates date of first switch
                                  and  last_prim_dt is NTV too*/
   keep bene_id last_prim_index_dt ;
  format last_prim_index_dt date9. ;
run;

proc sort data= swit;by bene_id;run;

data swit1;
   set swit;
   by bene_id;
   if last.bene_id;
run;/*get the list of ids with last prim index dt for switchers*/

*****non-switchers,  get date of last observation****;
data no_swit;
  set coh_ana5a;
  by bene_id;

  if last.bene_id and idswitch ne 1;
  last_prim_index_dt= rx_dt;

  format last_prim_index_dt date9. ;
  keep bene_id last_prim_index_dt;
run;
 
 proc sort data= swit1;by bene_id;run;
 proc sort data= no_swit; by bene_id ; run;
 proc sort data= coh_ana5a; by bene_id;run;


*****************analytical sampe*********;
data ra.coh_ana5a;
  merge coh_ana5a swit1 no_swit;
   by bene_id;
run;/*n=4305*/


*********************************************************************
*************get time between last rx and end of 2017/death************
**********************************************************************;

proc sort data= ra.coh_ana5a out=coh_ana5a; by bene_id rx_dt;run;

data last_rx;
set coh_ana5a;

by bene_id;

if last.bene_id;/*keep last last id for end of fu date*/

/*those who did not  died*/

if bene_death_dt = . then lastrx_end =  intck('days', rx_dt, '31DEC2017'd, 'D');
/*thos who died*/
 else if bene_death_dt ne . then lastrx_end =  intck('days', rx_dt, bene_death_dt, 'D');
 rename rx_dt= last_rx_dt;

keep bene_id ra_dt bene_death_dt rx_dt lastrx_end;
run;

***merge to include the new var***;

proc sort data= last_rx (keep= bene_id lastrx_end last_rx_dt); by bene_id;run;
proc sort data= ra.coh_ana5a out= coh_ana5a; by bene_id;run;

data ra.coh_ana5a;
 merge coh_ana5a last_rx;
 by bene_id;

*********create variable for start of FU row for interval change and cessation*******;

 ************time for part D*****;

index_lagrx_int= lag_rx_dt - index_dt;
/***time(days) from index30 dt for part b***/
  index30_lagrx_int= lag_rx_dt - index_30;

run;/*4306*/

